using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DM.DAO;
using DM.Entity;

namespace DM.Web
{
    public partial class BaoCao : System.Web.UI.Page
    {
        readonly TepDAO _tepDao = new TepDAO();
        private List<Tep> listBCTC = new List<Tep>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindingDropDowListNam();
                GetListBCTC();
                BindingGridViewBCTC();
            }           
        }

        //Tạo danh sách các báo cáo tài chính
        private void GetListBCTC()
        {
            listBCTC = _tepDao.GetTepBCTC();
        }


        
        private void BindingGridViewBCTC()
        {
            gridViewBCTC.DataSource = listBCTC;
            gridViewBCTC.DataBind();
        }


        private void BindingDropDowListNam()
        {
            List<Tep> listTep = new List<Tep>();
            listTep = _tepDao.GetTepBCTC();
            dlNam.Items.Clear();
            ListItem rootItem = new ListItem("Năm");
            dlNam.Items.Add(rootItem);
            int nam = 0;
            for (int i = 0; i< listTep.Count;i++)
            {
                if(listTep[i].Nam!=nam)
                {
                    dlNam.Items.Add(new ListItem(listTep[i].Nam.ToString()));
                    nam = listTep[i].Nam;
                }
            }
        }

        /// <summary>
        /// Chuyển đổi số tự nhiên sang số La Mã (khi hiển thị báo cáo tài chính)
        /// </summary>
        /// <param name="quy"></param>
        /// <returns></returns>
        public string convertQuy(string quy)
        {
            switch (Common.ParseInt32(quy))
            {
                case 1:
                    return "I";
                    break;
                case 2:
                    return "II";
                    break;
                case 3:
                    return "III";
                    break;
                default:
                    return "IV";
                    break;
            }
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            GetListBCTC();
            if (dlQuy.SelectedValue.Equals("0"))
            {           
                if(dlNam.SelectedItem.Text.Equals("Năm"))
                {
                    gridViewBCTC.DataSource = listBCTC;
                    gridViewBCTC.DataBind();
                }
                else
                {
                    List<Tep> newList = new List<Tep>();
                    foreach (Tep tep in listBCTC)
                    {
                        if (tep.Nam.ToString().Equals(dlNam.SelectedItem.Text))
                        {
                            newList.Add(tep);
                        }
                    }
                    gridViewBCTC.DataSource = newList;
                    gridViewBCTC.DataBind();
                }
            }
            else
            {
                if(dlNam.SelectedItem.Text.Equals("Năm"))
                {
                    List<Tep> newList = new List<Tep>();
                    foreach (Tep tep in listBCTC)
                    {
                        if(tep.Quy.ToString().Equals(dlQuy.SelectedValue))
                        {
                            newList.Add(tep);
                        }
                    }
                    gridViewBCTC.DataSource = newList;
                    gridViewBCTC.DataBind();
                }
                else
                {
                    List<Tep> newList = new List<Tep>();
                    foreach (Tep tep in listBCTC)
                    {
                        if (tep.Nam.ToString().Equals(dlNam.SelectedItem.Text) && tep.Quy.ToString().Equals(dlQuy.SelectedValue))
                        {
                            newList.Add(tep);
                        }
                    }
                    gridViewBCTC.DataSource = newList;
                    gridViewBCTC.DataBind();
                }
            }
        }
    }
}
